/*
 * 文 件    名:  BaseContentProvider.java
 * 创建日期:  2012-10-9下午3:44:30
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  cl073
 */
package com.weipu.common.facade.content;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;

import com.weipu.common.util.Logger;

/** 
 * 内容提供者基类
 * @version [版本号, 2012-10-9]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author cl073
 */

public class BaseContentProvider extends ContentProvider
{
    
    /**
     * tag for log
     */
    private static final String TAG = "BaseContentProvider";
    
    /**
     * 数据操作对象
     */
    private static DBHandler handler;
    
    protected static SQLiteDatabase dbReader;
    
    protected static SQLiteDatabase dbWriter;
    
    /**
     * 删除
     * @param uri
     * @param selection
     * @param selectionArgs
     * @return
     */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs)
    {
        //根据id进行操作
        if (ContentProviderFactory.isOperateById(uri))
        {
            return ContentProviderFactory.getContentProvier(uri).delete(uri,
                BaseColumns._ID + "=" + uri.getPathSegments().get(2),
                null);
        }
        
        return ContentProviderFactory.getContentProvier(uri).delete(uri, selection, selectionArgs);
    }
    
    /**
     * 自定义类型
     * @param uri
     * @return
     */
    @Override
    public String getType(Uri uri)
    {
        return ContentProviderFactory.getContentProvier(uri).getType(uri);
    }
    
    /**
     * 新增
     * @param uri
     * @param values
     * @return
     * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues)
     */
    @Override
    public Uri insert(Uri uri, ContentValues values)
    {
        return ContentProviderFactory.getContentProvier(uri).insert(uri, values);
    }
    
    /**
     * 初始化
     * @return
     */
    public boolean onCreate()
    {
        
        Logger.d(TAG, "enter into onCreate");
        
        handler = new DBHandler(getContext());
        dbReader = handler.getReadableDatabase();
        dbWriter = handler.getWritableDatabase();
        
        Logger.d(TAG, "exit from onCreate");
        return true;
    }
    
    /**
     * 查询
     * @param uri
     * @param projection
     * @param selection
     * @param selectionArgs
     * @param sortOrder
     * @return
     * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
     */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    {
        //根据id进行操作
        if (ContentProviderFactory.isOperateById(uri))
        {
            return ContentProviderFactory.getContentProvier(uri).query(uri,
                projection,
                BaseColumns._ID + "=" + uri.getPathSegments().get(2),
                null,
                sortOrder);
        }
        return ContentProviderFactory.getContentProvier(uri)
            .query(uri, projection, selection, selectionArgs, sortOrder);
    }
    
    /**
     * 更新
     * @param uri
     * @param values
     * @param selection
     * @param selectionArgs
     * @return
     * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
     */
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
    {
        //根据id进行操作
        if (ContentProviderFactory.isOperateById(uri))
        {
            return ContentProviderFactory.getContentProvier(uri).update(uri,
                values,
                BaseColumns._ID + "=" + uri.getPathSegments().get(2),
                selectionArgs);
        }
        return ContentProviderFactory.getContentProvier(uri).update(uri, values, selection, selectionArgs);
    }
    
    /**
     * 原生sql查询
     * @param sql
     * @param parameter
     * @return [参数说明]
     * @return Cursor [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public Cursor rawQuery(String sql, String... parameter)
    {
        return dbReader.rawQuery(sql, parameter);
    }
    
    /**
     * 删除搜索历史中第15数据之后的数据
     */
    public void deleteNoUsedData(String sql){
        dbWriter.execSQL(sql);
    }
}
